//
//  MyOrdersViewController.m
//  cityStar
//
//  Created by Hancong on 14/12/30.
//  Copyright (c) 2014年 tiny. All rights reserved.
//

#import "MyOrdersViewController.h"
#import "OrderTableViewCell.h"
#import "LineView.h"
#import "UIColor+FlatUI.h"
#import "OrderListSupplier.h"
#import "OrderListGood.h"
#import "SVPullToRefresh.h"
#import "UIAlertView+Blocks.h"
#import "UIActionSheet+Blocks.h"

NSString *const orderTableViewCellID = @"OrderTableViewCellID";

@interface MyOrdersViewController ()

@property (weak, nonatomic) IBOutlet NSLayoutConstraint *LineHorizontalContraint;
@property (nonatomic, weak) IBOutlet UITableView *tableView;
@property (nonatomic, strong) IBOutlet UIView *sectionHeadView;
@property (nonatomic, strong) IBOutlet UIView *sectionFooterView;
@property (nonatomic, weak) IBOutlet UIButton *btnselectAll;
@property (nonatomic, weak) IBOutlet UIButton *btnwaitPay;
@property (nonatomic, weak) IBOutlet UIButton *btnwaitGet;
@property (nonatomic, weak) IBOutlet UIButton *btnalredyGet;
@property (nonatomic, assign) int page;
@property (nonatomic, strong) NSMutableArray *requestArray;
@property (nonatomic, assign) KOrderStatues orderStatues;
@property (weak, nonatomic) IBOutlet UILabel *noLabel;

@end

@implementation MyOrdersViewController

-(instancetype)initWithSelectStatues:(BOOL)statues;
{
    self = [super init];
    if (self) {
        if (statues) {
            self.orderStatues = KOrderStatuesWaitGet;
        }else{
            self.orderStatues = KOrderStatuesWaitPay;
        }
    }
    return self;
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [self setUpTableView];
    if (!self.orderStatues) {
        self.orderStatues = KOrderStatuesSelectAll;
        [self webRequest_getOrderMessgaeWithPageMore:NO];
    }else if (self.orderStatues == KOrderStatuesWaitGet){
        [self waitGet_Touch:self.btnwaitGet];
    }else if (self.orderStatues == KOrderStatuesWaitPay){
        [self waitPay_Touch:self.btnwaitPay];
    }
    self.noLabel.hidden = YES;
    self.page = 1;
}

-(void)setUpTableView
{
    self.title = @"我的订单";
    [self addBackButton];
    [self.tableView registerNib:[OrderTableViewCell nib] forCellReuseIdentifier:orderTableViewCellID];
    
    __weak typeof(self)weakSelf = self;
    [self.tableView addPullToRefreshWithActionHandler:^{
        [weakSelf webRequest_getOrderMessgaeWithPageMore:NO];
    }];
    [self.tableView addInfiniteScrollingWithActionHandler:^{
        [weakSelf webRequest_getOrderMessgaeWithPageMore:YES];
    }];
    [self.tableView triggerPullToRefresh];
}

#pragma mark - WebRequest
-(void)webRequest_getOrderMessgaeWithPageMore:(BOOL)isMore
{
    NSString *postUrl;
    if (self.orderStatues == KOrderStatuesSelectAll) {
       postUrl  = [NSString stringWithFormat:@"%@%i",KInterface_getAllOrderList,isMore?self.page:0];
    }else if (self.orderStatues == KOrderStatuesWaitPay){
        postUrl  = [NSString stringWithFormat:@"%@%i",KInterface_getPayOrderList,isMore?self.page:0];
    }else if (self.orderStatues == KOrderStatuesWaitGet){
        postUrl  = [NSString stringWithFormat:@"%@%i",KInterface_getReceiveOrderList,isMore?self.page:0];
    }else if (self.orderStatues == KOrderStatuesAlredyGet){
        postUrl  = [NSString stringWithFormat:@"%@%i",KInterface_getReceiveOrderList,isMore?self.page:0];
    }
    
    [HttpRequestManager getMethod:postUrl parameters:nil completedMainBlock:^(BOOL success, id response, NSString *errMsg) {
        DLog(@"%@",response);
        if (success) {
            if (isMore) {
                NSMutableArray *responseArray = [[NSMutableArray alloc]initWithArray:response[@"value"][@"content"]];
                [responseArray enumerateObjectsUsingBlock:^(NSDictionary *obj, NSUInteger idx, BOOL *stop) {
                    [self.requestArray addObjectsFromArray:obj[@"supplier"]];
                }];
                self.page = self.page + 1;
            }else{
                self.requestArray = [[NSMutableArray alloc]init];
                NSMutableArray *responseArray = [[NSMutableArray alloc]initWithArray:response[@"value"][@"content"]];
                [responseArray enumerateObjectsUsingBlock:^(NSDictionary *obj, NSUInteger idx, BOOL *stop) {
                    [self.requestArray addObjectsFromArray:obj[@"supplier"]];
                }];
                DLog(@"%@",self.requestArray);
//                self.requestArray = [[NSMutableArray alloc]initWithArray:response[@"value"][@"content"]];
            }
            if (self.orderStatues == KOrderStatuesSelectAll) {
                if(self.requestArray.count == 0)
                {
                    self.noLabel.hidden = NO;
                }else{
                    self.noLabel.hidden = YES;
                }
            }
            else{
                self.noLabel.text = @"暂无该类型订单";
                if(self.requestArray.count == 0)
                {
                    self.noLabel.hidden = NO;
                }else{
                    self.noLabel.hidden = YES;
                }
            }
            [self.tableView reloadData];
        }else{
            if ([errMsg isEqualToString:@"没有此类订单"]) {
                [self.requestArray removeAllObjects];
                [self.tableView reloadData];
                self.noLabel.text = @"暂无该类型订单";
                if(self.requestArray.count == 0)
                {
                    self.noLabel.hidden = NO;
                }else{
                    self.noLabel.hidden = YES;
                }
            }
//            [SVProgressHUD showErrorWithStatus:errMsg];
        }
        if (isMore) {
            [self.tableView.infiniteScrollingView stopAnimating];
        }else{
            [self.tableView.pullToRefreshView stopAnimating];
        }
    }];
}

#pragma mark - Actions
-(IBAction)selectAll_Touch:(UIButton *)sender
{
    self.page = 1;
    self.orderStatues = KOrderStatuesSelectAll;
    [self webRequest_getOrderMessgaeWithPageMore:NO];
    [self.btnselectAll setTitleColor:[UIColor colorWithRed:0.973 green:0.514 blue:0.243 alpha:1.000] forState:UIControlStateNormal];
    [self.btnwaitPay setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [self.btnalredyGet setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [self.btnwaitGet setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    
    [self changeimagelineframe:CGRectGetMinX(sender.frame)];
}

-(IBAction)waitPay_Touch:(UIButton *)sender
{
    self.page = 1;
    self.orderStatues = KOrderStatuesWaitPay;
    [self webRequest_getOrderMessgaeWithPageMore:NO];
    [self.btnselectAll setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [self.btnwaitPay setTitleColor:[UIColor colorWithRed:0.973 green:0.514 blue:0.243 alpha:1.000] forState:UIControlStateNormal];
    [self.btnalredyGet setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [self.btnwaitGet setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [self changeimagelineframe:CGRectGetMinX(sender.frame)];
}

-(IBAction)waitGet_Touch:(UIButton *)sender
{
    self.page = 1;
    self.orderStatues = KOrderStatuesWaitGet;
    [self webRequest_getOrderMessgaeWithPageMore:NO];
    [self.btnselectAll setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [self.btnwaitPay setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [self.btnwaitGet setTitleColor:[UIColor colorWithRed:0.973 green:0.514 blue:0.243 alpha:1.000] forState:UIControlStateNormal];
    [self.btnalredyGet setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [self changeimagelineframe:CGRectGetMinX(sender.frame)];
}

-(IBAction)alredyGet_Touch:(UIButton *)sender
{
    self.page = 1;
    self.orderStatues = KOrderStatuesAlredyGet;
    [self webRequest_getOrderMessgaeWithPageMore:NO];
    [self.btnselectAll setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [self.btnwaitPay setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [self.btnwaitGet setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [self.btnalredyGet setTitleColor:[UIColor colorWithRed:0.973 green:0.514 blue:0.243 alpha:1.000] forState:UIControlStateNormal];
    [self changeimagelineframe:CGRectGetMinX(sender.frame)];
}

-(void)changeimagelineframe:(CGFloat)positionX
{
    [UIView animateWithDuration:0.2 animations:^{
        self.LineHorizontalContraint.constant = positionX;
        [self.view layoutIfNeeded];
    }];
}

-(void)btnCancelOrderWithUUID:(UIButton *)sender
{
    RIButtonItem *leftButton = [RIButtonItem itemWithLabel:@"确定" action:^{
        NSDictionary *dict = self.requestArray[sender.tag];
        OrderListSupplier *orderSupplier = [OrderListSupplier objectFromDictionary:dict];
//        orderSupplier.order
        
        [HttpRequestManager putMethod:KInterface_orderCancel parameters:@{@"uuid":orderSupplier.order} completedMainBlock:^(BOOL success, id response, NSString *errMsg) {
            if (success) {
                [SVProgressHUD showSuccessWithStatus:@"取消订单成功"];
                [self webRequest_getOrderMessgaeWithPageMore:NO];
            }else{
                [SVProgressHUD showSuccessWithStatus:errMsg];
            }
        }];
        
    }];
    RIButtonItem *rightButton = [RIButtonItem itemWithLabel:@"取消" action:^{
    }];
    UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"温馨提示" message:@"是否取消订单？" cancelButtonItem:leftButton otherButtonItems:rightButton, nil];
    [alertView show];
}

-(void)submitCommodity:(UIButton *)sender
{
    RIButtonItem *submitButton = [RIButtonItem itemWithLabel:@"确定" action:^{
        OrderListSupplier *orderSupplier = [OrderListSupplier objectFromDictionary:self.requestArray[sender.tag]];
        NSDictionary *parameters = @{@"uuid":orderSupplier.order};
    [HttpRequestManager putMethod:KInterface_CommodityAccept parameters:parameters completedMainBlock:^(BOOL success, id response, NSString *errMsg) {
    if (success) {
        [SVProgressHUD showSuccessWithStatus:@"确认收获成功"];
    }else{
        if ([response[@"code"] intValue] == 23001) {
            [SVProgressHUD showErrorWithStatus:@"没有待收货的商品"];
        }else if ([response[@"code"] intValue] == 23002) {
            [SVProgressHUD showErrorWithStatus:@"确认收货失败"];
        }
    }
    }];
    }];
    RIButtonItem *cancelButton = [RIButtonItem itemWithLabel:@"取消"];
    UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"是否确认收货" message:nil cancelButtonItem:cancelButton otherButtonItems:submitButton, nil];
    [alertView show];
    DLog(@"%@",self.requestArray[sender.tag]);
}

-(void)deleteOrder:(UIButton *)sender
{
    RIButtonItem *submitButton = [RIButtonItem itemWithLabel:@"确定" action:^{
        OrderListSupplier *orderSupplier = [OrderListSupplier objectFromDictionary:self.requestArray[sender.tag]];
        NSString *postUrl = [NSString stringWithFormat:@"%@%@",KInterface_CommodityRemove,orderSupplier.order];
        [HttpRequestManager deleteMethod:postUrl parameters:nil completedMainBlock:^(BOOL success, id response, NSString *errMsg) {
            if (success) {
                [SVProgressHUD showSuccessWithStatus:@"删除订单成功"];
            }else{
                [SVProgressHUD showErrorWithStatus:errMsg];
            }
        }];
    }];
    RIButtonItem *cancelButton = [RIButtonItem itemWithLabel:@"取消"];
    UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"是否确认删除订单" message:nil cancelButtonItem:cancelButton otherButtonItems:submitButton, nil];
    [alertView show];
}

#pragma mark - UITableViewDelegate and UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    // Return the number of sections.    
    return self.requestArray.count+1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (section < self.requestArray.count) {
        return 70;
    }else{
        return 0;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    if (section < self.requestArray.count) {
        OrderListSupplier *orderSupplier = [OrderListSupplier objectFromDictionary:self.requestArray[section]];
        if (orderSupplier.goods.count >1) {
            return 139;
        }else{
            return 109;
        }
    }else{
        return 0;
    }
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    NSDictionary *dict = self.requestArray[section];
    OrderListSupplier *orderSupplier = [OrderListSupplier objectFromDictionary:dict];
    
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(40, 20, 70, 60)];
    view.backgroundColor = [UIColor whiteColor];
    
    LineView *lineViewbelow = [[LineView alloc]initWithFrame:CGRectMake(0, 0, 320, 20)];
    lineViewbelow.backgroundColor = [UIColor colorFromHexCode:@"F2F1EC"];
    [view addSubview:lineViewbelow];
    
    UILabel *labelTitle = [[UILabel alloc]initWithFrame:CGRectMake(120, 10+20, 180, 30)];
    labelTitle.text = [NSString stringWithFormat:@"%@",orderSupplier.supplier_name];
    labelTitle.font = [UIFont systemFontOfSize:14];
    labelTitle.textAlignment = NSTextAlignmentRight;
    labelTitle.textColor = [UIColor darkGrayColor];
    [view addSubview:labelTitle];
    
    UILabel *labelstatues = [[UILabel alloc]initWithFrame:CGRectMake(20, 10+20, 120, 30)];
    /**
     *  关于订单状态：
     新增order_status:
     -2，已删除；-1，已取消；0，未付款；1，已付款，未发货；2，已付款部分发货；3，全部发货待收货；4，已确认收货；5，货到付款已付款；6，已退货；7，商家确认收货；8，已退款；9，货到付款，未发货
     */
    switch ([orderSupplier.order_status intValue]) {
        case -2:
            labelstatues.text = @"已删除";
            break;
        case -1:
            labelstatues.text = @"已取消";
            break;
        case 0:
            labelstatues.text = @"待付款";
            break;
        case 1:
            labelstatues.text = @"未发货";
            break;
        case 2:
            labelstatues.text = @"待收货";
            break;
        case 3:
            labelstatues.text = @"待收货";
            break;
        case 4:
            labelstatues.text = @"待评价";
            break;
        case 5:
            labelstatues.text = @"已付款";
            break;
        case 6:
            labelstatues.text = @"待评价";
            break;
        case 7:
            labelstatues.text = @"待评价";
            break;
        case 8:
            labelstatues.text = @"已退款";
            break;
        case 9:
            labelstatues.text = @"未发货";
            break;
        case 10:
            labelstatues.text = @"待使用";
            break;
        case 11:
            labelstatues.text = @"待评价";
            break;
            
        default:
            break;
    }
    labelstatues.textColor = [UIColor orangeColor];
    labelstatues.font = [UIFont systemFontOfSize:15];
    [view addSubview:labelstatues];
    
    LineView *lineViewdown = [[LineView alloc]initWithFrame:CGRectMake(15, 10+59, 320, 0.5)];
    lineViewdown.backgroundColor = [UIColor colorWithWhite:0.925 alpha:1.000];
    [view addSubview:lineViewdown];
    
    LineView *lineViewtop = [[LineView alloc]initWithFrame:CGRectMake(0, 20+0, 320, 0.5)];
    lineViewtop.backgroundColor = [UIColor colorWithWhite:0.925 alpha:1.000];
    [view addSubview:lineViewtop];
    if (section < self.requestArray.count) {
        return view;
    }else{
        return nil;
    }
}

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    __block int number = 0;
    __block float money = 0;
    OrderListSupplier *orderSupplier = [OrderListSupplier objectFromDictionary:self.requestArray[section]];
    [orderSupplier.goods enumerateObjectsUsingBlock:^(OrderListGood *obj, NSUInteger idx, BOOL *stop) {
        money = money +[obj.number floatValue]*[obj.now_price floatValue];
        number = number + [obj.number intValue];
    }];
    
    float heightOfView = 0.0f;
    BOOL isExpand = NO;
    if (orderSupplier.goods.count > 1) {
        isExpand = YES;
    }
    
    if (isExpand == YES) {
        heightOfView = 30.0f;
    }else{
        heightOfView = 0.0f;
    }
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(40, 20, 109+heightOfView, 60)];
    view.backgroundColor = [UIColor whiteColor];
    UIImageView *lineViewtop1 = [[UIImageView alloc]initWithFrame:CGRectMake(0, -1, 320, 1)];
    lineViewtop1.backgroundColor = [UIColor colorFromHexCode:@"ECECEC"];
    [view addSubview:lineViewtop1];
    UIImageView *imagearrow = [[UIImageView alloc]initWithFrame:CGRectMake(87, 5, 30, heightOfView)];
    imagearrow.image = [UIImage imageNamed:@"arrow_down"];
    [view addSubview:imagearrow];
    
    UIButton *btnOpen = [[UIButton alloc]initWithFrame:CGRectMake(60, 6, 200, heightOfView)];
    [btnOpen setTitle:@"展开其他商品" forState:UIControlStateNormal];
    [btnOpen setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
    btnOpen.titleLabel.font = [UIFont systemFontOfSize:15];
    [view addSubview:btnOpen];
    
    UIImageView *lineViewtop2 = [[UIImageView alloc]initWithFrame:CGRectMake(0, 10+heightOfView, 320, 1)];
    lineViewtop2.backgroundColor = [UIColor colorFromHexCode:@"ECECEC"];
    [view addSubview:lineViewtop2];
    if (isExpand == YES) {
        imagearrow.hidden = NO;
        btnOpen.hidden = NO;
        lineViewtop2.hidden = NO;
    }else{
        imagearrow.hidden = YES;
        btnOpen.hidden = YES;
        lineViewtop2.hidden = YES;
    }
    
    UILabel *labelTotal = [[UILabel alloc]initWithFrame:CGRectMake(71, 18+heightOfView, 200, 25)];
    labelTotal.text = [NSString stringWithFormat:@"共计%i件商品     合计：",number];
    labelTotal.font = [UIFont systemFontOfSize:17];
    labelTotal.textColor = [UIColor darkGrayColor];
    [view addSubview:labelTotal];
    
    UILabel *labelMoney = [[UILabel alloc] initWithFrame:CGRectMake(220, 18+heightOfView, 86, 25)];
    labelMoney.text = [NSString stringWithFormat:@"￥%.2f",money];
    labelMoney.textColor = [UIColor colorWithWhite:0.200 alpha:1.000];
    labelMoney.textAlignment = NSTextAlignmentRight;
    labelMoney.font = [UIFont systemFontOfSize:18];
    [view addSubview:labelMoney];
    
    UIImageView *lineViewtop3 = [[UIImageView alloc]initWithFrame:CGRectMake(20, 55+heightOfView, 300, 1)];
    lineViewtop3.backgroundColor = [UIColor colorFromHexCode:@"ECECEC"];
    [view addSubview:lineViewtop3];
    
    if ([orderSupplier.order_status intValue] == 0) {
        UIButton *btnCancelOrder = [UIButton buttonWithType:UIButtonTypeCustom];
        [btnCancelOrder setTitle:@"取消订单" forState:UIControlStateNormal];
        [btnCancelOrder setTitleColor:[UIColor colorWithWhite:0.200 alpha:1.000] forState:UIControlStateNormal];
        [btnCancelOrder setBackgroundImage:[UIImage imageNamed:@"四个字及以下字数"] forState:UIControlStateNormal];
        [btnCancelOrder setBackgroundImage:[UIImage imageNamed:@"四个字及以下字数_touch"] forState:UIControlStateHighlighted];
        btnCancelOrder.frame = CGRectMake(155, 72+heightOfView, 65, 25);
        btnCancelOrder.titleLabel.font = [UIFont systemFontOfSize:14];
        btnCancelOrder.tag = section;
        [btnCancelOrder addTarget:self action:@selector(btnCancelOrderWithUUID:) forControlEvents:UIControlEventTouchUpInside];
        [view addSubview:btnCancelOrder];
        
        UIButton *btnPayOrder = [UIButton buttonWithType:UIButtonTypeCustom];
        [btnPayOrder setTitle:@"付款" forState:UIControlStateNormal];
        [btnPayOrder setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
        [btnPayOrder setBackgroundImage:[UIImage imageNamed:@"付款"] forState:UIControlStateNormal];
        [btnPayOrder setBackgroundImage:[UIImage imageNamed:@"付款_touch"] forState:UIControlStateHighlighted];
        btnPayOrder.frame = CGRectMake(245, 72+heightOfView, 65, 25);
        btnPayOrder.titleLabel.font = [UIFont systemFontOfSize:14];
        btnPayOrder.layer.borderColor = [UIColor orangeColor].CGColor;
        [view addSubview:btnPayOrder];
        
    }else if ([orderSupplier.order_status intValue] == 3 ||[orderSupplier.order_status intValue] == 2) {
        UILabel *labelOrderNo = [[UILabel alloc]initWithFrame:CGRectMake(12, 74+heightOfView, 200, 21)];
        labelOrderNo.text = [NSString stringWithFormat:@"订单号：%@",orderSupplier.order];
        labelOrderNo.textColor = [UIColor darkGrayColor];
        labelOrderNo.font = [UIFont systemFontOfSize:16];
        [view addSubview:labelOrderNo];
        
        UIButton *btnSubmit = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        [btnSubmit setTitle:@"确认收货" forState:UIControlStateNormal];
        btnSubmit.frame = CGRectMake(235, 72+heightOfView, 68, 25);
        btnSubmit.titleLabel.font = [UIFont systemFontOfSize:14];
        btnSubmit.layer.cornerRadius = 3;
        btnSubmit.clipsToBounds = YES;
        btnSubmit.layer.borderWidth = 1.0f;
        btnSubmit.layer.borderColor = [UIColor colorWithRed:0.612 green:0.776 blue:0.996 alpha:1.000].CGColor;
        btnSubmit.tag = section;
        [btnSubmit addTarget:self action:@selector(submitCommodity:) forControlEvents:UIControlEventTouchUpInside];
        [view addSubview:btnSubmit];
        
        UIImageView *lineViewlast = [[UIImageView alloc]initWithFrame:CGRectMake(0, 108+heightOfView, 320, 1)];
        lineViewlast.backgroundColor = [UIColor colorFromHexCode:@"ECECEC"];
        [view addSubview:lineViewlast];
    }else if ([orderSupplier.order_status intValue] == -1) {
        UIButton *btnDeleteOrder = [UIButton buttonWithType:UIButtonTypeCustom];
        [btnDeleteOrder setTitle:@"删除订单" forState:UIControlStateNormal];
        [btnDeleteOrder setTitleColor:[UIColor colorWithWhite:0.151 alpha:1.000] forState:UIControlStateNormal];
        [btnDeleteOrder setBackgroundImage:[UIImage imageNamed:@"四个字及以下字数"] forState:UIControlStateNormal];
        [btnDeleteOrder setBackgroundImage:[UIImage imageNamed:@"四个字及以下字数_touch"] forState:UIControlStateHighlighted];
        btnDeleteOrder.tag = section;
        [btnDeleteOrder addTarget:self action:@selector(deleteOrder:) forControlEvents:UIControlEventTouchUpInside];
        btnDeleteOrder.frame = CGRectMake(235, 72+heightOfView, 68, 25);
        btnDeleteOrder.titleLabel.font = [UIFont systemFontOfSize:14];
        [view addSubview:btnDeleteOrder];
    }else if ([orderSupplier.order_status intValue] == 1) {
        UIButton *btnReturnMoney = [UIButton buttonWithType:UIButtonTypeCustom];
        [btnReturnMoney setTitle:@"退款" forState:UIControlStateNormal];
        [btnReturnMoney setTitleColor:[UIColor colorWithWhite:0.151 alpha:1.000] forState:UIControlStateNormal];
        [btnReturnMoney setBackgroundImage:[UIImage imageNamed:@"四个字及以下字数"] forState:UIControlStateNormal];
        [btnReturnMoney setBackgroundImage:[UIImage imageNamed:@"四个字及以下字数_touch"] forState:UIControlStateHighlighted];
        btnReturnMoney.frame = CGRectMake(235, 72+heightOfView, 68, 25);
        btnReturnMoney.titleLabel.font = [UIFont systemFontOfSize:14];
        [view addSubview:btnReturnMoney];
    }else{
        UILabel *labelOrderNo = [[UILabel alloc]initWithFrame:CGRectMake(12, 74+heightOfView, 200, 21)];
        labelOrderNo.text = [NSString stringWithFormat:@"订单号：%@",orderSupplier.order];
        labelOrderNo.textColor = [UIColor darkGrayColor];
        labelOrderNo.font = [UIFont systemFontOfSize:16];
        [view addSubview:labelOrderNo];
    }
    
    if (section < self.requestArray.count) {
        return view;
    }else{
        return nil;
    }
}


-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section < self.requestArray.count) {
        OrderListSupplier *orderSupplier = [OrderListSupplier objectFromDictionary:self.requestArray[indexPath.section]];
        OrderListGood *orderGood = orderSupplier.goods[indexPath.row];
        OrderTableViewCell *orderTableViewCell = [tableView dequeueReusableCellWithIdentifier:orderTableViewCellID];
        orderTableViewCell.selectionStyle = UITableViewCellSelectionStyleGray;
        [orderTableViewCell configWithGood:orderGood];
        return orderTableViewCell;
    }else{
        static NSString *CellIdentifier = @"AddOrderFramTableViewCell";
        
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
        }
        cell.backgroundColor = [UIColor colorWithRed:0.945 green:0.941 blue:0.910 alpha:1.000];
        return cell;
    }
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section < self.requestArray.count) {
        return [OrderTableViewCell heightOfTableCell];
    }else{
        return 160;
    }
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section < self.requestArray.count) {
//        OrderListSupplier *orderSupplier = [OrderListSupplier objectFromDictionary:self.requestArray[section][@"supplier"][sec]];
        OrderListSupplier *orderSupplier = [OrderListSupplier objectFromDictionary:self.requestArray[section]];
        return orderSupplier.goods.count;
    }else{
        return 1;
    }
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView reloadData];
}

//去掉UItableview headerview黏性
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
//    DLog(@"%f",scrollView.contentOffset.y);
    if (scrollView == self.tableView)
    {
        CGFloat sectionHeaderHeight = 70;
        if (scrollView.contentOffset.y<= sectionHeaderHeight&&scrollView.contentOffset.y>=0) {
            scrollView.contentInset = UIEdgeInsetsMake(-scrollView.contentOffset.y, 0, 0, 0);
        } else if (scrollView.contentOffset.y>=sectionHeaderHeight) {
            scrollView.contentInset = UIEdgeInsetsMake(-sectionHeaderHeight, 0, 0, 0);
        }
    }
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
